
//
//  PMineCollectViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/23.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineCollectViewController.h"
#import "LYTitleView.h"
#import "JobCell.h"
#import "CompanyJobModel.h"
#import "CompanyInfoModel.h"
#import "CompanyCell.h"
#import "JobDetailViewController.h"
#import "CompanyDetailViewController.h"

@interface PMineCollectViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *jobDataArray;
    NSMutableArray *companyDataArray;
    
    NSInteger currentPageJob;
    NSInteger currentPageCompany;
    
    LYTitleView *titleView;
}
@property (nonatomic, strong) UITableView *jobCompanyTableView;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation PMineCollectViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"收藏关注";
    
    jobDataArray = [NSMutableArray arrayWithCapacity:0];
    companyDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPageJob = 1;
    currentPageCompany = 1;
    
    [self initUI];
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSString *url = PersonMine_GetFavoriteJob_Lists_URL;
    NSString *page = @"1";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    switch (self.selectIndex) {
        case 0: { // 职位
            url = PersonMine_GetFavoriteJob_Lists_URL;
            page = [NSString stringWithFormat:@"%ld", (long)currentPageJob];
        } break;
        case 1: { // 公司
            url = PersonMine_GetFocusJob_Lists_URL;
            page = [NSString stringWithFormat:@"%ld", (long)currentPageCompany];
        } break;
        default:
            break;
    }
    [dic setObject:page forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:url params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dic in data) {
                    switch (self.selectIndex) {
                        case 0: {
                            CompanyJobModel *model = [[CompanyJobModel alloc] init];
                            [model setValuesForKeysWithDictionary:dic];
                            [self->jobDataArray addObject:model];
                        } break;
                        case 1: {
                            CompanyInfoModel *model = [[CompanyInfoModel alloc] init];
                            [model setValuesForKeysWithDictionary:dic];
                            [self->companyDataArray addObject:model];
                        } break;
                        default:
                            break;
                    }
                }
                [self.jobCompanyTableView reloadData];
            }
            switch (self.selectIndex) {
                case 0: {
                    if (self->jobDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfJobCollect superView:self.view];
                    }
                } break;
                case 1: {
                    if (self->companyDataArray.count) {
                    } else {
                        self.noDataView.hidden = NO;
                        [self.noDataView vcNilStatusType:VCNilStatusTypeOfCompanyCollect superView:self.view];
                    }
                } break;
                default:
                    break;
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    NSArray *titleArray = @[@"职位", @"公司"];
    self.selectIndex = 0;
    titleView = [[LYTitleView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40*Scale) titlesArray:titleArray norCorlor:THEMECOLOR_OF_TEXT_666666 selectColor:THEMECOLOR_OF_ORANGE];
    __weak typeof(self)weakSelf = self;
    titleView.block = ^(NSInteger index){
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSLog(@"%li",index);
        strongSelf.selectIndex = index;
        if (index) { // 公司
            if (self->companyDataArray && self->companyDataArray.count) {
                strongSelf.noDataView.hidden = YES;
                [strongSelf.jobCompanyTableView reloadData];
            } else {
                [strongSelf loadData];
            }
        } else { // 职位
            if (self->jobDataArray && self->jobDataArray.count) {
                strongSelf.noDataView.hidden = YES;
                [strongSelf.jobCompanyTableView reloadData];
            } else {
                [strongSelf loadData];
            }
        }
    };
    [self.view addSubview:titleView];
    
    self.jobCompanyTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, EndY(titleView), WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(titleView)) target:self style:UITableViewStylePlain];
    self.jobCompanyTableView.tableFooterView = [[UIView alloc] init];
    self.jobCompanyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.jobCompanyTableView];
    
    self.jobCompanyTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        switch (self.selectIndex) {
            case 0: {
                self->currentPageJob++;
            } break;
            case 1: {
                self->currentPageCompany++;
            } break;
            default:
                break;
        }
        [weakSelf.jobCompanyTableView.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (self.selectIndex) {
        case 0: { // 职位
            if (jobDataArray.count) {
                return jobDataArray.count;
            }
        } break;
        case 1: { // 公司
            if (companyDataArray.count) {
                return companyDataArray.count;
            }
        } break;
        default:
            break;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (self.selectIndex) {
        case 0: { // 职位
            JobCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jobCellID"];
            if (!cell) {
                cell = [[JobCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"jobCellID"];
            }
            
            if (jobDataArray.count) {
                CompanyJobModel *model = jobDataArray[indexPath.row];
                [cell reloadDataModelForCollect:model];
            }
            return cell;
        } break;
        case 1: { // 公司
            CompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"companyCellID"];
            if (!cell) {
                cell = [[CompanyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"companyCellID"];
            }
            
            if (companyDataArray.count) {
                CompanyInfoModel *model = companyDataArray[indexPath.row];
                [cell reloadDataModelForCollect:model];
            }
            return cell;
        } break;
        default:
            break;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (self.selectIndex) {
        case 0: {
            CompanyJobModel *model = jobDataArray[indexPath.row];
            JobDetailViewController *JDVC = [[JobDetailViewController alloc] init];
            JDVC.id = model.job_id;
            JDVC.showSettingFlag = NO;
            JDVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:JDVC animated:YES];
        } break;
        case 1: {
            CompanyInfoModel *model = companyDataArray[indexPath.row];
            CompanyDetailViewController *CDVC = [[CompanyDetailViewController alloc] init];
            CDVC.id = model.id;
            CDVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:CDVC animated:YES];
        } break;
        default:
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (self.selectIndex) {
        case 0: {
            return JobCellHeight;
        } break;
        case 1: {
            CompanyInfoModel *model = companyDataArray[indexPath.row];
            return model.listCellHeight;
        } break;
        default:
            break;
    }
    return 0.00001;
}
- (void)noNetButtonTap {
    [self.navigationController popViewControllerAnimated:NO];
    UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    switch (self.selectIndex) {
        case 0: {
            tabBarController.selectedIndex = 0;
        } break;
        case 1: {
            tabBarController.selectedIndex = 1;
        } break;
        default:
            break;
    }
}

@end
